package com.xawl.controller;

import com.xawl.pojo.CourseLearning;
import com.xawl.pojo.PopularNav;
import com.xawl.pojo.User;
import com.xawl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description: 跟登录相关的接口
 * @Author: qin jin
 * @Date: 2023/4/7 16:05
 */
@Controller
public class LoginController {

    @Autowired
    @Qualifier("userServiceImpl")
    private UserService userService;

    // 登录验证
    @RequestMapping("/login")
    @ResponseBody
    public String login(@RequestParam("username") String username, @RequestParam("password") String password, HttpServletRequest req) {
        User user = userService.checkLogin(username, password);
        String result;
        if (user != null) {
            req.getSession().setAttribute("user", user);
            result = "ok";
        } else {
            result = "error";
        }
        return result;
    }

    // 注册用户
    @RequestMapping("/register")
    @ResponseBody
    public String register(@RequestParam("username") String username, @RequestParam("password") String password) {
        // 检查用户名是否重复
        User user2 = userService.queryUserByName(username);
        if (user2 != null) {
            return "Duplicate username";
        }
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        int success = userService.registerUser(user);

        String result;
        if (success >= 1) {
            result = "ok";
        } else {
            result = "error";
        }
        return result;
    }

    // 进入首页 -> 加载数据 (热门导航信息)
    @RequestMapping("/welcome")
    public String loginCheck(HttpServletRequest req) {
        //查询数据
        List<PopularNav> pnList = userService.queryAllNav();
        List<CourseLearning> cList = userService.queryAllCourse();
        req.getSession().setAttribute("cList", cList);
        req.getSession().setAttribute("pnList", pnList);
        return "red-page/welcome";
    }

    // 进入后台管理检查管理员身份
    @RequestMapping("/checkAdmin")
    @ResponseBody
    public String checkAdmin(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        String result;
        if (user.getType().equals("系统管理员")) {
            result = "ok";
        } else {
            result = "error";
        }
        return result;
    }

    // 退出登录
    @RequestMapping("/logout")
    public String logout(HttpServletRequest req) {
        User user = (User)req.getSession().getAttribute("user");
        req.getSession().removeAttribute("user");
        return "redirect: /login";
    }
}
